package com.kyle.pagingdemo.room

import androidx.paging.PagingSource
import androidx.room.*
import com.kyle.pagingdemo.ArticleEntity
import com.kyle.pagingdemo.data.model.Page
import com.kyle.pagingdemo.data.model.Post

/**
 * author: liliang
 * date: 1/6/21 8:58 PM
 * desc:
 */
@Dao
interface ArticleDao {

    @Query("SELECT * FROM article order by id desc")
    fun allArticle(): PagingSource<Int, ArticleEntity>

    @Insert
    fun insert(article: ArticleEntity)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertArticles(articles: List<ArticleEntity>)

    @Delete
    fun delete(article: ArticleEntity)

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertPages(page: List<Page>)

    @Query("select * from Page where id = :id")
    fun getPageById(id: Int): Page?

    @Query("DELETE FROM Page")
    fun clearPage()

    @Query("SELECT * FROM Post")
    fun allPost(): PagingSource<Int, Post>

    @Query("SELECT COUNT(*) FROM Post order by id desc")
    fun getLocalPostSize(): Int

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertPosts(articles: List<Post>)

    @Query("DELETE FROM Post")
    fun clearPost()

}